package com.apache.flow.core.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.apache.core.orm.Page;
import com.apache.flow.core.dao.FlowAutomaticProcessingDao;
import com.apache.flow.core.entity.FlowAutomaticProcessing;
import com.apache.tools.StrUtil;

@Service
@Transactional
public class FlowAutomaticProcessingManager {
	@Autowired
	private FlowAutomaticProcessingDao flowAutomaticProcessingDao;

	/**
	 * 
	 * @param taskId
	 * @return
	 * <p>@description 根据taskId获取自处理对象
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午04:45:58
	 */
	@Transactional(readOnly = true)
	public FlowAutomaticProcessing getAutomaticProcessingByNodeId(String taskId) {
		return flowAutomaticProcessingDao.findUniqueBy("taskId", taskId);
	}

	/**
	 * 
	 * @param flowAutomaticProcessing
	 * <p>@description 添加或者修改自处理任务对象
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午04:46:32
	 */
	public void saveOrUpdateAutomaticProcessing(FlowAutomaticProcessing flowAutomaticProcessing) {
		flowAutomaticProcessingDao.save(flowAutomaticProcessing);
	}

	/**
	 * 
	 * @param flowAutomaticProcessing
	 * <p>@description 删除自处理对象
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午04:47:07
	 */
	public void deleteAutomaticProcessing(FlowAutomaticProcessing flowAutomaticProcessing) {
		flowAutomaticProcessingDao.delete(flowAutomaticProcessing);
	}

	/**
	 * 
	 * @param status
	 * @return
	 * <p>@description 根据状态获取自处理记录
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午04:48:19
	 */
	@Transactional(readOnly = true)
	public List<FlowAutomaticProcessing> getFlowAutomaticProcessingList(String status) {
		String hql = "from FlowAutomaticProcessing where handleStauts=?";
		return flowAutomaticProcessingDao.find(hql, status.trim());
	}

	/**
	 * 
	 * @param status,executeTime
	 * @return
	 * <p>@description 根据状态和执行时间获取自处理记录
	 */
	@Transactional(readOnly = true)
	public List<FlowAutomaticProcessing> getFlowAutoListByStatusAndTime(String status, String executeTime) {
		String hql = "from FlowAutomaticProcessing where handleStauts=? and (executeTime <=? or executeTime is null)";
		return flowAutomaticProcessingDao.find(hql, status.trim(), executeTime);
	}

	/**
	 * 
	 * <p>@description 分页+条件查询自处理记录
	 */
	@Transactional(readOnly = true)
	public Page<FlowAutomaticProcessing> getFlowAutomaticProcessingPage(Page<FlowAutomaticProcessing> page,
			Map<String, String> map) {
		StringBuffer hql = new StringBuffer("from FlowAutomaticProcessing f where 1=1 ");
		String taskId = map.get("taskId");
		String handleStauts = map.get("handleStauts");
		String processInsId = map.get("processInsId");

		if (StrUtil.isNotEmptyWithTrim(taskId)) {
			hql.append(" and f.taskId = '").append(taskId.trim()).append("' ");
		}
		if (StrUtil.isNotEmptyWithTrim(handleStauts)) {
			hql.append(" and f.handleStauts = '").append(handleStauts.trim()).append("' ");
		}
		if (StrUtil.isNotEmptyWithTrim(processInsId)) {
			hql.append(" and f.processInsId = '").append(processInsId.trim()).append("' ");
		}

		hql.append(" order by f.createTime desc ");
		return flowAutomaticProcessingDao.findPage(page, hql.toString(), new HashMap<String, String>());
	}

	/**
	 * 
	 * @param processInsId 流程实例Id
	 * @param status 状态
	 * <p>@description 根据流程实例Id修改自处理表中对应实例任务的状态
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-8-21 下午05:32:38
	 */
	public int updateAutomaticProcessingStatus(String processInsId, String status) {
		return flowAutomaticProcessingDao.updateAutomaticProcessingStatus(processInsId, status);
	}

	/**
	 * 
	 * @param processInsId 流程实例Id
	 * @return
	 * <p>@description 根据实例Id删除流程实例的自处理任务
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-8-21 下午05:42:53
	 */
	public int deleteAutomaticProcessingByProcessInsId(String processInsId) {
		return flowAutomaticProcessingDao.deleteAutomaticProcessingByProcessInsId(processInsId);
	}

	public boolean checkAutoTaskIfValid(String processInsId) {
		return flowAutomaticProcessingDao.checkAutoTaskIfValid(processInsId);
	}
}
